package com.example.arwhack.data.model;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.arwhack.R;

import java.text.SimpleDateFormat;
import java.util.List;

public class TransactionRecordAdapter extends ArrayAdapter<TransactionRecord> {

    private int resourceId;
    private Context context;

    public TransactionRecordAdapter(Context context, int textViewResourceId, List<TransactionRecord> objects) {
        super(context, textViewResourceId, objects);
        this.resourceId = textViewResourceId;
        this.context = context;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent){
        TransactionRecord transactionRecord = getItem(position);
        LinearLayout item = new LinearLayout(getContext());
        String inflater = Context.LAYOUT_INFLATER_SERVICE;
        LayoutInflater vi = (LayoutInflater)getContext().getSystemService(inflater);
        vi.inflate(resourceId, item, true);
        //买入还是卖出
        TextView sellOrBuyText = (TextView)item.findViewById(R.id.sellOrBuyText);
        if("sell".equalsIgnoreCase(transactionRecord.getTransactionType())){
            sellOrBuyText.setText("卖出");
            sellOrBuyText.setTextColor(getContext().getResources().getColor(R.color.colorGreen));
        }else{
            sellOrBuyText.setText("买入");
            sellOrBuyText.setTextColor(getContext().getResources().getColor(R.color.colorRed));
        }
        //货币
        TextView coinAndCurrencyText = (TextView)item.findViewById(R.id.coinAndCurrencyText);
        coinAndCurrencyText.setText(String.format("%s/%s",transactionRecord.getCurrency(),transactionRecord.getVirtualCoin()));
        //单价
        TextView commissionPriceText = (TextView)item.findViewById(R.id.commissionPriceText);
        commissionPriceText.setText(String.format("委托价格:%.4f",transactionRecord.getUnitPrice()));
        //委托金额
        TextView commissionMoneyText = (TextView)item.findViewById(R.id.commissionMoneyText);
        commissionMoneyText.setText(String.format("委单金额:%.4f",transactionRecord.getCommissionMoney()));
        //实际金额
        TextView completeMoneyText = (TextView)item.findViewById(R.id.CompleteMoneyText);
        completeMoneyText.setText(String.format("实际金额:%.4f",transactionRecord.getCompleteMoney()));
        //委托数量
        TextView commissionMountText = (TextView)item.findViewById(R.id.commissionMountText);
        commissionMountText.setText(String.format("委托数量:%.2f",transactionRecord.getCommissionAmount()));
        //实际数量
        TextView completeMountText2 = (TextView)item.findViewById(R.id.completeMountText2);
        completeMountText2.setText(String.format("实际数量:%.2f",transactionRecord.getCompleteAmount()));
        //交易时间
        String date = new SimpleDateFormat("yyyy-MM-dd HH:mm").format(transactionRecord.getTradeTime());
        TextView tradeTimeText = (TextView)item.findViewById(R.id.tradeTimeText);
        tradeTimeText.setText(date);
        return item;
    }

}
